package com.darkplanet.management.dao;

import com.darkplanet.management.domain.FirstMenu;
import com.darkplanet.management.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class FirstMenuDao implements IFirstMenuDao {

    QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDs());

    @Override
    public FirstMenu getFirstMenuById(Integer id) {
        String sql = "select * from firstmenu where f_menu_id = ?";
        FirstMenu firstMenu = null;
        try {
            firstMenu = queryRunner.query(sql, new BeanHandler<FirstMenu>(FirstMenu.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return firstMenu;
    }

    @Override
    public List<FirstMenu> getFirstMenus() {
        String sql = "select * from firstmenu";
        List<FirstMenu> firstMenus = null;
        try {
            firstMenus = queryRunner.query(sql, new BeanListHandler<FirstMenu>(FirstMenu.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return firstMenus;
    }

    @Override
    public void addFirstMenu(String f_menu_name, String f_menu_path, String remark) {
        String sql = "insert into firstmenu (f_menu_name,f_menu_path,remark) values (?,?,?)";
        try {
            queryRunner.update(sql,f_menu_name,f_menu_path,remark);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteFirstMenuById(Integer id) {
        String sql = "delete from firstmenu where f_menu_id = ?";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateFirstMenuById(Integer id) {
        String sql = "update from firstmenu set f_menu_name,f_menu_path,remark where f_menu_id = ?";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
